<?php //-->
/*
 * This file is part a custom application package.
 * (c) 2011-2012 Christian Blanquera <cblanquera@gmail.com>
 */

/**
 * Default logic to output a page
 */
class Front_Page_Index extends Front_Page {
	/* Constants
	-------------------------------*/
	const RANGE = 50;
	
	/* Public Properties
	-------------------------------*/
	/* Protected Properties
	-------------------------------*/
	protected $_title = 'Support';
	protected $_class = 'support';
	protected $_template = '/index.phtml';
	
	protected $_meta = array(
		'title' 			=> 'Support',
		'subject'	 		=> 'Support',
		'copyright'			=> '2012-2013',
		'keywords' 			=> 'support, community',
		'classification' 	=> 'support, community',
		'revisit' 			=> '2 days',
		'revisit-after' 	=> '2 days',
		'description' 		=> 'Support Threads',
		'abstract' 			=> 'Support Threads');
	
	/* Private Properties
	-------------------------------*/
	/* Magic
	-------------------------------*/
	/* Public Methods
	-------------------------------*/
	public function render() {
		$sort 	= front()->registry()->get('get', 'sort');
		$page 	= front()->registry()->get('get', 'page');
		$query 	= front()->registry()->get('get', 'q');
		
		$this->_body['summarize'] 	= true;
		
		//create a common search
		$search = front()
			->post()
			->search()
			->setColumns('post1.*', 'user.*', 
				'COALESCE(SUM(vote_value), 0) as votes',
				'(SELECT COUNT(*) as answers FROM post as post2
				WHERE post2.post_type=\'answer\' AND post2.post_parent=post1.post_id
				AND (post2.post_active > 0 OR post2.post_active BETWEEN -1 AND -4)) 
				AS answers')
			->setTable('post AS post1')
			->innerJoinOn('user', 'user_id=post1.post_user')
			->leftJoinOn('vote', 'vote_post=post1.post_id')
			->addFilter('post1.post_type=%s', 'question')
			->addFilter('(post1.post_active = 1 OR post1.post_active BETWEEN -2 AND -1)')
			->setGroup('post1.post_id')
			->sortByVotes('DESC')
			->setRange(self::RANGE);
		
		//if this is a search query
		if(trim($query)) {
			//set the query and filter
			$this->_body['query'] = $query;
			$search->addFilter('(post_title LIKE %s OR post_detail LIKE %s)', '%'.$query.'%', '%'.$query.'%');
		}
		
		//case for sorting
		switch($sort) {
			case 'new':
				$search->sortByPostCreated('DESC');
				break;
			case 'old':
				$search->sortByPostCreated('ASC');
				break;
			case 'unanswered':
				$search->sortByPostAnswers('ASC');
				break;
			default: 
				$search->sortByVotes('DESC');
				break;
		}
		
		//case for pagination
		if(is_numeric($page)) {
			$search->setPage($page);
		} else {
			$page = 1;
		}
		
		//now generate the final collection
		$this->_body['collection'] = $search->getCollection();
		
		//add extra collection details
		foreach($this->_body['collection'] as $post) {
			$author 				= front()->user()->model($post->getPostUser());
			$post['user_name'] 		= $author->getUserName();
			$post['user_facebook'] 	= $author->getAttributes('facebook_id');
			$post['user_picture'] 	= $author->getAttributes('picture');
			$post['tags']			= $post->getCategories();
		}
		
		//create a pagination block
		$this->_body['pagination'] = front()->template(__DIR__.'/_pagination.phtml', jquery()
			->block()
			->pagination($search->getTotal())
			->setRange(self::RANGE)
			->setPage($page)
			->setUrl('/')
			->setQuery($_GET)
			->getVariables());
		
		
		return $this->_page();
	}
	
	/* Protected Methods
	-------------------------------*/
	/* Private Methods
	-------------------------------*/
}
